﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using control.Biz;

public partial class usercontrol_Cart : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
            if (cart.Items.Count == 0)
            {
                lbThongBao.Text = "Không có sản phẩm trong giỏ hàng";
                btCheckout.Enabled = false;
            }
            else
            {
                GridView1.DataSource = cart.Items;
                GridView1.DataBind();
                //GridView1.FooterRow.Cells[5].Text = cart.TongTien();
            }
        }
    }
    public string lblThanhTienValue()
    {
        BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
        return cart.TongTien();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        int indexItem = cart.FindIndexItem(ID);
        cart.Items.RemoveAt(indexItem);
        Session["cart"] = cart;
        GridView1.DataSource = cart.Items;
        GridView1.DataBind();
        GridView1.FooterRow.Cells[5].Text = cart.TongTien();
        if (cart.Items.Count == 0)
        {
            lbThongBao.Text = "Không có sản phẩm trong giỏ hàng";
            btCheckout.Enabled = false;
        }
        
        
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        int newSoluong = 0;
        try { newSoluong = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSoLuong")).Text); }
        catch { }
        int indexItem = cart.FindIndexItem(ID);
       
        cart.Items[indexItem].SoLuong = newSoluong;
        Session["cart"] = cart;
        GridView1.EditIndex = -1;
        GridView1.DataSource = cart.Items;
        GridView1.DataBind();
        GridView1.FooterRow.Cells[5].Text = cart.TongTien();
    }
    protected void btContinue_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btCheckout_Click(object sender, EventArgs e)
    {
        if (Session["account"] == null)
            lbThongBao.Text = "bạn phải đăng nhập trước khi mua";
        else
        {
            BizKhachHang kh = (BizKhachHang)Session["account"];
            BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
            if (KiemTraSoLuong(cart) == true)
                lbThongBao.Text = "Số lương tồn kho không đủ, xin hãy kiểm tra lại phiếu mua hàng";
            else
            {
                cart.KhachHang = kh;
                DateTime begintime = new DateTime(2011, 11, 11);
                long ticktime = DateTime.Now.Ticks - begintime.Ticks;
                TimeSpan _timeSpan = new TimeSpan(ticktime);
                string maPX = "PXH" + _timeSpan.TotalSeconds.ToString();
                cart.Ma = maPX;
                cart.NgayXuat = DateTime.Now;
                cart.Insert();
                if (cart.ID != -1)
                {
                    lbThongBao.Text = "Thanh toán thành công";
                    btCheckout.Enabled = false;
                    cart = new BizPhieuXuatHang();
                    Session["cart"] = cart;
                }
                else
                    lbThongBao.Text = "Việc mua không  thực hiên được";
                GridView1.DataSource = cart.Items;
                GridView1.DataBind();
                GridView1.FooterRow.Cells[5].Text = cart.TongTien();
            }
        }
    }

    private static bool KiemTraSoLuong(BizPhieuXuatHang cart)
    {
        foreach (BizChiTietPhieuXuat item in cart.Items)
        {
            if (item.SoLuong > item.TonKho)
                return true;
        }
        return false;
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
        GridView1.DataSource = cart.Items;
        GridView1.DataBind();
        GridView1.FooterRow.Cells[5].Text = cart.TongTien();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BizPhieuXuatHang cart = (BizPhieuXuatHang)Session["cart"];
        GridView1.DataSource = cart.Items;
        GridView1.DataBind();
        GridView1.FooterRow.Cells[5].Text = cart.TongTien();
    }
}
